using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;
using Telerik.WinControls.UI;
using NHibernate.Collection.Generic;
namespace LionForms
{
    public partial class MainForm : Telerik.WinControls.UI.RadForm
    {

        public MainForm()
        {
            InitializeComponent();
            lblVoucherNumber.Text = (0).ToString();
            dtpVoucherDate.Value = DateTime.Today;
            tabVoucher.IsSelected = true;

            GridViewComboBoxColumn gvcbxAccounts = (GridViewComboBoxColumn)gvDataSheet.Columns["Account"];
            gvcbxAccounts.DataSource = Lion.Account.All.Values.ToList();
            gvcbxAccounts.DisplayMember = "VisualName";
            gvcbxAccounts.ValueMember = "ID";
        }

        private void MainForm1_Load(object sender, EventArgs e)
        {

        }
        private void InitializeCellFonts()
        {
            #region CellSize

            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).IsCurrent = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).IsCurrentColumn = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).IsSorted = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).IsPinned = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).IsSelected = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).Text = "";
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).Font = new System.Drawing.Font("Segoe UI", 12.25F);
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(1))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).IsCurrent = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).IsCurrentColumn = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).IsSorted = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).IsPinned = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).IsSelected = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).Text = "";
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).Font = new System.Drawing.Font("Segoe UI", 12.25F);
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(2))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).IsCurrent = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).IsCurrentColumn = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).IsSorted = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).IsPinned = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).IsSelected = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).Text = "";
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).Font = new System.Drawing.Font("Segoe UI", 12.25F);
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(3))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).IsCurrent = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).IsCurrentColumn = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).IsSorted = true;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).IsPinned = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).IsSelected = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).TextAlignment = System.Drawing.ContentAlignment.MiddleRight;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).Text = "";
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).Font = new System.Drawing.Font("Segoe UI", 12.25F);
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(4))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).IsCurrent = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).IsCurrentColumn = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).IsSorted = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).IsPinned = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).IsSelected = false;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).TextAlignment = System.Drawing.ContentAlignment.MiddleRight;
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).Text = "";
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).Font = new System.Drawing.Font("Segoe UI", 12.25F);
            ((Telerik.WinControls.UI.GridDataCellElement)(this.gvDataSheet.GetChildAt(0).GetChildAt(1).GetChildAt(1).GetChildAt(5))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
            #endregion
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            DialogResult userChoice = RadMessageBoxExtensions.Show("Save Data?", "SAVE", MessageBoxButtons.OKCancel);
            if (userChoice == DialogResult.OK)
            {
                SaveJournalData();
            }

        }

        private void SaveJournalData()
        {
            gvDataSheet.Refresh();

            var rows = gvDataSheet.Rows;
            Lion.Voucher Voucher = new Lion.Voucher();

            DateTime DOI = dtpVoucherDate.Value;
            Voucher.DOI = DOI;

            ISet<Lion.JournalEntry> entries = new HashSet<Lion.JournalEntry>();


            foreach (var row in rows)
            {

                var record = new Lion.JournalEntry();
                string accountID = row.Cells["Account"].Value.ToString().Trim();
                if (!string.IsNullOrEmpty(accountID))
                {
                    record.Description = row.Cells["Description"] == null ? null : row.Cells["Description"].Value.ToString();
                    record.Reference = row.Cells["Reference"] == null ? null : row.Cells["Reference"].Value.ToString();
                    record.TransactionID = row.Cells["TransID"] == null ? null : row.Cells["TransID"].Value.ToString();
                    record.Debit = decimal.Parse(row.Cells["Debit"].Value.GetType() == typeof(DBNull) ? "0" : row.Cells["Debit"].Value.ToString());
                    record.Credit = decimal.Parse(row.Cells["Credit"].Value.GetType() == typeof(DBNull) ? "0" : row.Cells["Credit"].Value.ToString());
                    record.Voucher = Voucher;

                    record.Account = Lion.Account.All[accountID];
                    record.AccountClass = record.Account.AccountCategory.AccountClass;
                    record.DOI = DOI;
                    entries.Add(record);
                }

            }


            decimal creditSum = entries.Sum(c => c.Credit);
            decimal debiteSum = entries.Sum(c => c.Debit);

            if (creditSum != debiteSum)
            {
                RadMessageBoxExtensions.Show("Check your Debit and Credit ", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);

                return;
            }

            Exception ex;
            Lion.ObjectAssembler.Save(Voucher, out ex);
            Lion.ObjectAssembler.BulkInsert(entries, out ex, entries.Count);

            if (ex != null)
            {

                RadMessageBoxExtensions.Show("Error while Saving Voucher", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
            else
            {
                RadMessageBoxExtensions.Show("Saved Successfuly");
            }

        }

        private void btnLoadReport_Click(object sender, EventArgs e)
        {
            DateTime from = dtpFrom.Value;
            DateTime to = dtpTo.Value;
            if (from > to)
            {

                RadMessageBoxExtensions.Show("'From' cannot be greater than 'To' Date!!!", "Error", MessageBoxButtons.OK, RadMessageIcon.Error);
                return;
            }
            ReportType reportType = ReportType.GeneralJournal;
            Telerik.WinControls.UI.RadButton btn = (Telerik.WinControls.UI.RadButton)sender;

            switch (btn.Name)
            {
                case "btnGJournalReport":
                    reportType = ReportType.GeneralJournal;
                    break;
                case "btnGLedgerReport":
                    reportType = ReportType.GeneralLedger;
                    break;
                case "btnBalanceSheet":
                    reportType = ReportType.BalanceSheet;
                    break;
            }

            new Report(reportType, from, to).Visible = true;
        }

        private void AccountstabItem_Click(object sender, EventArgs e)
        {
            new AccountsForms().Visible = true;
        }

        private void tabVoucher_Click(object sender, EventArgs e)
        {
            GridViewComboBoxColumn gvcbxAccounts = (GridViewComboBoxColumn)gvDataSheet.Columns["Account"];
            gvcbxAccounts.DataSource = Lion.Account.All.Values.ToList();
            gvcbxAccounts.DisplayMember = "VisualName";
            gvcbxAccounts.ValueMember = "ID";
        }

       
    }
}
